package com.example.zhouzhaocheng;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.apache.commons.lang3.StringEscapeUtils;

public class MainActivity3 extends AppCompatActivity {

    private String url = "https://tianqiapi.com/api?version=v1&appid=65716696&appsecret=pPQF7EgL&city=";
    private EditText cityE;
    private EditText weather;
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        cityE = findViewById(R.id.cityE);
        weather = findViewById(R.id.weather);
        recyclerView = findViewById(R.id.recyclerView);
    }

    private void postByOkGo(String url) {
        OkGo.<String>get(url)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String newValue = StringEscapeUtils.unescapeJava(response.body());
                        try {
                            WeatherBean localBean =new Gson().fromJson(newValue,WeatherBean.class);
                            weather.setText(
                                    "日期："+localBean.data.get(0).date+"\n" +
                                    "天气："+localBean.data.get(0).tem2+"~"+localBean.data.get(0).tem1+"\n" +
                                    "风向："+localBean.data.get(0).win.get(0)+"\n" +
                                    "空气："+localBean.data.get(0).air_level+"\n" +
                                    "提示："+localBean.data.get(0).air_tips
                            );
                            recyclerView.setVisibility(View.VISIBLE);
                            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity3.this));
                            recyclerView.setAdapter(adapter);
                            adapter = new BaseRecyclerAdapter() {
                                @Override
                                protected void onBindView(@NonNull BaseViewHolder holder, @NonNull int position) {
                                    WeatherBean.Hour datum = localBean.data.get(0).hours.get(position);
                                    TextView name = holder.getView(R.id.textView);
                                    name.setText(datum.hours+","+datum.wea+"，"+datum.tem+"，"+datum.win+"，"+datum.win_speed);
                                }

                                @Override
                                protected int getLayoutResId(int position) {
                                    return R.layout.item_la;
                                }

                                @Override
                                public int getItemCount() {
                                    return localBean.data.get(0).hours.size();
//                                    return 0;
                                }
                            };
                        }catch (Throwable e0){
                            Log.e("ywh",e0.getMessage());
                        }

                    }
                });
    }

    public void search(View view) {
        postByOkGo(url + cityE.getText().toString());
    }
}